﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Reading
namespace backoffice.Data
{
    partial class Reading
    {
        public void setReading(Reading r)
        {
            this.ReadingID = r.ReadingID;
            this.LandID = r.LandID;
            this.pH = r.pH;
            this.Phosphorus = r.Phosphorus;
            this.Potassium = r.Potassium;
            this.Nitrogen = r.Nitrogen;
            this.Date = r.Date;
        }
    }
}
#endregion

#region dataReading
namespace backoffice.Data
{
    public class dataReading
    {
        #region Create
        public static void createReading(Reading r)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                r.ReadingID = Guid.NewGuid();
                r.Date = DateTime.Now;

                db.Readings.InsertOnSubmit(r);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Reading getReading(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Reading res = db.Readings.FirstOrDefault(r => r.ReadingID == guid);

                return res;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Reading> getReadings(Expression<Func<Reading, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Readings.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeReading(Reading r)
        {
            try
            {
                if (r == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Reading res = db.Readings.FirstOrDefault(read => read.ReadingID == r.ReadingID);

                    if (res == null) return false;
                    else
                    {
                        res.setReading(r);
                        
                        db.SubmitChanges();
                        
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteReading(Reading r)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Readings.Contains(r))
                {
                    Reading res = db.Readings.FirstOrDefault(read => read.ReadingID == r.ReadingID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        db.Readings.DeleteOnSubmit(res);
                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion